package net.oschina.app.ui;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import net.oschina.app.R;
import net.oschina.app.adapter.BaseRecyclerViewAdapter;
import net.oschina.app.bean.Active;
import net.oschina.app.bean.ActiveList;
import net.oschina.app.constant.Constants;
import net.oschina.app.util.BindingUtils;
import net.oschina.app.util.UIUtils;
import net.oschina.app.util.URLGet;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by yzp on 2017/4/1.
 */
public class FriendActivty extends Activity {

    private List<Active> datas = new ArrayList();
    private XRecyclerView mRecyclerView;
    private MyAdapter mAdapter;
    private Handler handler = new Handler();
    private Toolbar toolbar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friendfragment_activty);
        mRecyclerView = (XRecyclerView) this.findViewById(R.id.friend_xRecy);
        LinearLayoutManager layoutManager = new LinearLayoutManager(UIUtils.getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
       /* //设置下拉刷新样式
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallTrianglePath);*/

        mRecyclerView.setPullRefreshEnabled(false);
        toolbar = (Toolbar) findViewById(R.id.tl_title);
        toolbar.setNavigationIcon(R.drawable.actionbar_back_icon_normal);
        toolbar.setTitle("朋友圈");
        toolbarOnClick();
        intdata();

    }

    private void toolbarOnClick() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void intdata() {
        String path = Constants.Friends_datail_itemPath;
        URLGet urlGet = new URLGet();
        urlGet.getData(path, ActiveList.class, new URLGet.OnURLGetLitener<ActiveList>() {
            @Override
            public void showSuccess(ActiveList bean) {
                datas = bean.getActivelist();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        mAdapter = new MyAdapter(datas);
                        mRecyclerView.setAdapter(mAdapter);
                        mAdapter.notifyDataSetChanged();
                    }
                });
            }

            @Override
            public void showError(Exception e) {
            }
        });
    }
}

class MyAdapter extends BaseRecyclerViewAdapter {
    public List<Active> datas = new ArrayList<>();

    public MyAdapter(List<Active> datas) {
        this.datas = datas;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.friends_items_details, parent, false);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder holder1 = (ViewHolder) holder;
        Active active = datas.get(position);
        Glide.with(UIUtils.getContext()).load(active.getPortrait()).override(UIUtils.dip2Px(50),UIUtils.dip2Px(50)).into(holder1.ivUserd);
        BindingUtils.typeLoader(holder1.tvEvents, active.getObjectType() + "");
        holder1.tvContents.setText(Html.fromHtml(active.getMessage()));
        holder1.tvDates.setText(active.getPubDate());
        holder1.tvNames.setText(active.getAuthor());

    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class ViewHolder extends XRecyclerView.ViewHolder {

        public ImageView ivUserd;
        public TextView tvDates;
        public TextView tvEvents;
        public TextView tvContents;
        public TextView tvNames;
        //private final TextView tvPhones;

        public ViewHolder(View itemView) {
            super(itemView);
            ivUserd = (ImageView) itemView.findViewById(R.id.iv_users);
            tvDates = (TextView) itemView.findViewById(R.id.tv_dates);
            tvEvents = (TextView) itemView.findViewById(R.id.tv_events);
            tvContents = (TextView) itemView.findViewById(R.id.tv_contents);
            tvNames = (TextView) itemView.findViewById(R.id.tv_names);
            // tvPhones = (TextView) findViewById(R.id.tv_phone);
        }
    }

}
